package com.wanglibao.woodtrade.woodtrade.ui.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.entity.MyDemandEntitiy;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ItemLvMyDemandActivityAdapter extends BaseAdapter {

    public List<MyDemandEntitiy.ListEntity> objects = new ArrayList<>();
   
    private Context context;
    private LayoutInflater layoutInflater;

    public ItemLvMyDemandActivityAdapter(Context context,List<MyDemandEntitiy.ListEntity> list) {
        this.context = context;
        objects=list;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void setObjects(List<MyDemandEntitiy.ListEntity> objects) {
        this.objects = objects;
        notifyDataSetChanged();
    }
    public void remove(int position){
        objects.remove(position);
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public MyDemandEntitiy.ListEntity getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_lv_my_demand_activity, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((MyDemandEntitiy.ListEntity) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(MyDemandEntitiy.ListEntity object, ViewHolder holder) {
        if(object.getStatus().equals("1")){
            holder.rlItemLvYifabu.setBackgroundResource(R.color.background);
        }
        holder.tvItemLvQgTitle.setText(object.getTitle());
        holder.tvItemLvNameWood.setText(object.getCategory_name());
        holder.tvItemLvQgGuige.setText(object.getSize());
        holder.tvItemLvData.setText(object.getCreate_time().substring(0,10));
    }

    static class ViewHolder {
        @Bind(R.id.tv_item_lv_qg_title)
        TextView tvItemLvQgTitle;
        @Bind(R.id.tv_item_lv_name_wood)
        TextView tvItemLvNameWood;
        @Bind(R.id.tv_item_lv_guige)
        TextView tvItemLvGuige;
        @Bind(R.id.tv_item_lv_qg_guige)
        TextView tvItemLvQgGuige;
        @Bind(R.id.tv_item_lv_data)
        TextView tvItemLvData;
        @Bind(R.id.rl_item_lv_yifabu)
        RelativeLayout rlItemLvYifabu;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
